package longrise.phone.com.bjjt_henan.myaccident;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.longrise.android.FormParameter;
import com.longrise.android.LFView;
import com.longrise.bjjt.util.UiUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

import longrise.phone.com.bjjt_henan.R;
import longrise.phone.com.bjjt_henan.Utils.CommontUtils;
import longrise.phone.com.bjjt_henan.widget.HackyViewPager;
import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by Administrator on 2016/6/1.
 */
public class PhotoImageView extends LFView implements ViewPager.OnPageChangeListener {

    private Context context;

    private View view;

    private HackyViewPager viewPager;

    private LinearLayout dotLayout;

    private List<String> picLists;

    private int curItem = 0;

    public PhotoImageView(Context context, List<String> list) {
        super(context);
        this.context = context;
        this.picLists = list;
    }

    public void setCurItem(int curItem) {
        this.curItem = curItem;
    }

    @Override
    public View getView() {
        return view;
    }

    @Override
    public void init() {
        if (null == context) {
            return;
        }
        view = LayoutInflater.from(context).inflate(R.layout.view_photoview, null);
        if (null == view) {
            return;
        }
        viewPager = (HackyViewPager) view.findViewById(R.id.photoview_vp);
        dotLayout = (LinearLayout) view.findViewById(R.id.photoview_layout);

        initValues();
    }

    private void initValues() {
        if (null == picLists) {
            return;
        }
        //实例化指示点
        for (int i = 0; i < picLists.size(); i++) {
            View view = new View(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(UiUtil.dip2Px(8), UiUtil.dip2Px(8));
            if (i != 0) {
                params.leftMargin = 20;
            }
            view.setLayoutParams(params);
            view.setBackgroundResource(R.drawable.selector_dot);
            dotLayout.addView(view);
        }

        ImageAdapter adapter = new ImageAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(curItem);
        viewPager.addOnPageChangeListener(this);

        updateCurDot(curItem);
    }

    @Override
    public void onPageSelected(int position) {
        updateCurDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 更新选中点
     */
    private void updateCurDot(int pos) {
        for (int i = 0; i < dotLayout.getChildCount(); i++) {
            dotLayout.getChildAt(i).setEnabled(i == pos);
        }
    }

    @Override
    public FormParameter getFormParameter() {
        FormParameter form = new FormParameter();
        form.setShowtitle(false);
        return form;
    }

    @Override
    public void refresh() {

    }

    @Override
    public void refreshByTime() {

    }

    private class ImageAdapter extends PagerAdapter {

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public int getCount() {
            return (null == picLists) ? 0 : picLists.size();
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            PhotoView photoView = new PhotoView(container.getContext());
            photoView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            String url = picLists.get(position);
            ImageLoader.getInstance().displayImage(url, photoView, CommontUtils.getOptions());
            photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
                @Override
                public void onPhotoTap(View view, float x, float y) {
                    OnDestroy();// 关闭界面
                    closeForm();
                }

                @Override
                public void onOutsidePhotoTap() {

                }
            });
            // Now just add PhotoView to ViewPager and return it
            container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            return photoView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

    }

    @Override
    public void OnDestroy() {
        super.OnDestroy();
        view = null;
        if (null != viewPager) {
            viewPager.removeOnPageChangeListener(this);
            viewPager.setAdapter(null);
            viewPager = null;
        }
        dotLayout = null;
        if (null != picLists) {
            picLists.clear();
            picLists = null;
        }
    }
}
